﻿using System;
using System.Collections.Generic;

namespace Core
{
    public class SkillTriggerComponent : Entity, IAwake
    {
        private readonly Dictionary<string, Type> dict = new Dictionary<string, Type>();
        void IAwake.Awake()
        {
            var types = World.GetComponent<CodeTypes>().GetTypes(typeof(SkillTriggerAttribute));
            foreach ((string _, Type type) in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(SkillTriggerAttribute), false);
                if (attrs.Length == 0)
                    continue;

                dict[type.Name] = type;
            }
        }

        public ISkillTrigger CreateSkillTrigger(string triggerName, ISkill skill)
        {
            if (string.IsNullOrEmpty(triggerName))
                return CreateSkillTrigger("NoneSkillTrigger", skill);

            if(dict.TryGetValue(triggerName, out var type))
            {
                var trigger = Activator.CreateInstance(type) as ISkillTrigger;
                if (trigger != null)
                {
                    trigger.SetSkill(skill);
                }
                return trigger;
            }
            return default;
        }
    }
}